package servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Models.Customer;
import Models.Product;

public class ProductServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String action = req.getParameter("action") != null? req.getParameter("action") : "";
		if(action.equals("new")){
			getServletContext().getRequestDispatcher("/jsp/product/new.jsp").forward(req, resp);
		}else if(action.equals("show")){//show
			String id = req.getParameter("id");
			try {
				req.setAttribute("product", Product.loadSql(Integer.parseInt(id)));
			} catch (NumberFormatException e) {e.printStackTrace();}
			
			getServletContext().getRequestDispatcher("/jsp/product/show.jsp").forward(req, resp);
		
		}else if(action.equals("edit")){//edit
			String id = req.getParameter("id");
			try {
				req.setAttribute("product", Product.loadSql(Integer.parseInt(id)));
			} catch (NumberFormatException e) {e.printStackTrace();}
			
			getServletContext().getRequestDispatcher("/jsp/product/edit.jsp").forward(req, resp);
		
		}else if(action.equals("delete")){//edit
			String id = req.getParameter("id");
			try {
				Product product = Product.loadSql(Integer.parseInt(id));
				Product.delete(product);
			} catch (NumberFormatException e) {e.printStackTrace();}
			
			req.setAttribute("list_products", Product.loadSql());
			getServletContext().getRequestDispatcher("/jsp/product/index.jsp").forward(req, resp);
		}else{//index
			req.setAttribute("list_products", Product.loadSql());
			getServletContext().getRequestDispatcher("/jsp/product/index.jsp").forward(req, resp);
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
		String action = req.getParameter("action") != null? req.getParameter("action") : "";
		if(action.equals("new")){
			System.out.println("create");
			
			try {
				Product product = new Product();
				product.setName(req.getParameter("name"));
				product.setPrice(Float.parseFloat(req.getParameter("price")));
				product.setQuantity(Integer.parseInt(req.getParameter("quantity")));
				if(Product.save(product)){
					resp.sendRedirect("/SiteERP/product/");
					return;
				}
					
			} catch (NumberFormatException e) {e.printStackTrace();}
			
			req.setAttribute("msg", "Echec cr&eacute;ation produit");
			doGet(req, resp);
		}else if(action.equals("edit")){
			int id = Integer.parseInt(req.getParameter("id"));
			Product product = Product.loadSql(id);
			String new_name = req.getParameter("name");
			String new_price = req.getParameter("price");
			int quantity = Integer.parseInt(req.getParameter("quantity"));
			
			product.setName(new_name);
			product.setPrice(Float.parseFloat(new_price));
			product.setQuantity(quantity);
			if(product.update()){
				resp.sendRedirect("/SiteERP/product/");
				return;
			}else{
				doGet(req, resp);
			}
			
		}else{//index
			doGet(req, resp);
		}
		
	}
	
	
}
